package com.dream.model.app;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.annotations.Type;

import com.dream.vo.api.ApiUser;

@Entity
@Table(name="t_app_user")
public class AppUser extends AppAbstractUser implements Serializable {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8220325296767307711L;
	@Column(name="display")
	@Type(type = "org.hibernate.type.NumericBooleanType")
	private boolean display;
	
	@Transient
	private AppOrg firstOrg;
	
	@Transient
	private AppOrg secondOrg;
	
	public boolean isDisplay() {
		return display;
	}

	public void setDisplay(boolean display) {
		this.display = display;
	}

	public AppOrg getFirstOrg() {
		return firstOrg;
	}

	public void setFirstOrg(AppOrg firstOrg) {
		this.firstOrg = firstOrg;
	}

	public AppOrg getSecondOrg() {
		return secondOrg;
	}

	public void setSecondOrg(AppOrg secondOrg) {
		this.secondOrg = secondOrg;
	}
	
	public void initOrgs(List<AppOrg> li){
		
		if(CollectionUtils.isNotEmpty(li)){
			for(AppOrg org :li){
				if(org.getLevel().equals(1)){firstOrg = org;}
				if(org.getLevel().equals(2)){secondOrg = org;}
			}
		}
	}
	
	public ApiUser toApiUser(){
		
		ApiUser user = new ApiUser();
		user.setId(getId());
		user.setName(getName());
		user.setMobile(this.getMobile());
		if(firstOrg != null){
			user.setFirstOrg(firstOrg.getName());
		}
		if(secondOrg != null){
			user.setSecondOrg(secondOrg.getName());
		}
		return user;
	}
	
}
